
import unittest
import collections

class TestDeque(unittest.TestCase):
    def test_deque(self):
        aqueue = collections.deque(["cheka","stasi"])
        
        aqueue.append("kgb")
        self.assertEqual(["cheka","stasi","kgb"],list(aqueue))
        
        self.assertEqual("cheka",aqueue.popleft())
        self.assertEqual(["stasi","kgb"],list(aqueue))
        
        # pop up at the right terminal
        self.assertEqual("kgb",aqueue.pop())
        self.assertEqual(["stasi"],list(aqueue))
        
        # test exception when it is empty
        aqueue.pop()
        self.assertRaises(IndexError,lambda : aqueue.pop())
        
class MySingleTypeList(list):
    def __init__(self,atype):
        self.__type = atype
        list.__init__(self)
    
    def append(self,value):
        if isinstance(value,self.__type):
            list.append(self,value)
        else:
            raise TypeError("this list only limited for type<%s>"%(self.__type.__name__))
        
class TestUserdefCollection(unittest.TestCase):
    def test_limitation(self):
        intlist = MySingleTypeList(int)
        intlist.append(1)
        self.assertRaises(TypeError,lambda : intlist.append("cheka"))
        self.assertRaises(TypeError,lambda : intlist.append(3.14))
        
    def test_same_api(self):
        """
        demonstrate that APIs remain the same
        """
        strlist = MySingleTypeList(str)
        strlist.append("cheka")
        strlist.append("stasi")
        
        self.assertEqual(2,len(strlist))
        self.assertEqual("cheka",strlist[0])
        self.assertEqual("stasi",strlist[1])

if __name__ == "__main__":
    unittest.main()